<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>组织机构管理</title>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<link href="../../pages/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="../../pages/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
<link rel="stylesheet" href="../../pages/bootstrap/animate.min.css">
<link rel="stylesheet" href="../../pages/common/styles/common.css">
<script src="../../pages/jquery/jquery-1.12.4.min.js"></script>
<script src="../../pages/bootstrap/js/bootstrap.min.js"></script>
<script src="../../pages/common/js/bootstrap-grid.js"></script>
<script src="../../pages/common/js/common.js"></script>
<link rel="stylesheet" href="../ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="../ztree/js/jquery.ztree.core.js"></script>
</head>
<body class="animated fadeInRight">
	<div class="tabBar">
		<div class="tab">
			<img src="../../pages/images/tab_org_manager.png">
			组织机构管理
		</div>
		<div class="tab">
			<img src="../../pages/images/tab_org_add.png">
			组织机构新建
		</div>
		<div class="tab">
			<img src="../../pages/images/tab_org_update.png">
			组织机构修改
		</div>
		<div class="tab">
			<img src="../../pages/images/tab_org_auth.png">
			管理员授权
		</div>
	</div>
	<div class="tab-main tab-active">
		<div id="leftTree" style="width: 20%;float: left;">
			<div style="height: 30px;background-color: #e5f6fe;width: 98%;line-height: 30px;">
				组织机构树
			</div>
			<ul id="orgTree" class="ztree" ></ul>
		</div>
		<div style="width: 80%;height: 100%;float: left">
			<div>
				<a id="btnAdd" class="a-btn-blue"><span class="glyphicon glyphicon-plus font-size-12px"></span>&nbsp;新增</a>
				<a id="btnEdit" class="a-btn-green"><span class="glyphicon glyphicon-pencil font-size-12px"></span>&nbsp;修改</a>
				<a id="btnDel" class="a-btn-red"><span class="glyphicon glyphicon-remove font-size-12px"></span>&nbsp;删除</a>
				
				<a id="advancedSearchbtnOrg" class="a-btn-gray2 inline pull-right" style="color: #03a9f4;text-decoration: none">
				<span id="advancedSearchOrgText">高级搜索</span>&nbsp;
				<span id="advancedSearchOrgSpan" class="glyphicon glyphicon-chevron-down" title="高级搜索"></span></a>
			
				<div class="pull-right input-group" role="group" style="width: 30%;margin-right: 10px">
					<input id="searchFieldOrg" type="text" class="form-control" placeholder="输入机构编号或名称" aria-describedby="basic-addon2" >
					<span class="input-group-addon search-cusor" id="btnSearchOrg"><img alt="" src="../../pages/images/search.png"></span>
				</div>
			</div>
			
			<div id="advancedSearchOrg" style="margin-top:10px; display: none;">
				<table style="float: right;margin-right: 100px;margin-top: 10px;">
					<tr>
						<td style="padding-right: 10px;">
						</td>
						<td style="width: 300px;">
							<input id="searchParentOrgCode" type="text" class="form-control" style="width: 250px;" placeholder="请输入归属机构编号">
						</td>
						<td style="padding-right: 10px;">
						</td>
						<td style="width: 300px;">
							<input id="searchParentOrgName" type="text" class="form-control" style="width: 250px;" placeholder="请输入归属机构名称">
						</td>
						<td style="padding-right: 10px;">
						</td>
						<td style="width: 300px;">
							<input id="searchOrgShortName" type="text" class="form-control" style="width: 250px;" placeholder="请输入机构简称">
						</td>
					</tr>
				</table>
			</div>

			<div id="tab-main">
				<table id="orgManagerTable" class="table table-striped table-hover">
				  <thead>
				    <tr>
				      <th field="parentOrgCode">归属机构编号</th>
				      <th field="parentOrgName">归属机构名称</th>
				      <th field="orgCode">机构编号</th>
				      <th field="orgName">机构名称</th>
				      <th field="orgShortName">机构简称</th>
				      <th field="orgDuty">责任描述</th>
				      <th field="enableDate">启动时间</th>
				    </tr>
				  </thead>
				  <tbody>
				  </tbody>
				</table>
				<div style="margin-bottom: 10px;">
				  <ul id="orgManagerTable_page" class="pagination">
				  	<!-- 
				    <li><a>上一页</a></li>
				    <li class="active"><a>1</a></li>
				    <li><a>2</a></li>
				    <li><a>3</a></li>
				    <li><a>4</a></li>
				    <li><a>5</a></li>
				    <li><a>6</a></li>
				    <li><a>7</a></li>
				    <li><a>8</a></li>
				    <li><a>9</a></li>
				    <li><a>10</a></li>
				    <li><a>下一页</a></li>
				    -->
				  </ul>
	   			  <div class="pull-right" style="margin-top: 25px;">
				   	<!-- 显示分页参数  -->
				  </div>
			 	</div>
			</div>
		</div>
		
	</div>
	<div class="tab-main">
		
		<table class="table-form" style="width: 100%;">
			<tr style="display: none;">
				<th width="10%">级别</td>
				<td width="40%">
					<input id="level" type="text" class="form-control inline"  aria-describedby="basic-addon1">
				</td>
			</tr>
			<tr>
				<th width="10%">机构编号</td>
				<td width="40%">
					<input id="orgCode" type="text" class="form-control inline"  aria-describedby="basic-addon1">
				</td>
				<th width="10%"><font color="red">*</font>机构名称</th>
				<td width="40%">
					<input id="orgName" type="text" class="form-control"  aria-describedby="basic-addon1">
				</td>
			</tr>
			<tr>
				<th width="10%"><font color="red">*</font>机构简称</th>
				<td width="40%">
					<input id="orgShortName" type="text" class="form-control"  aria-describedby="basic-addon1">
				</td>
				<th width="10%">归属机构名称</th>
				<td width="40%">
					<input id="parentOrgName" type="text" class="form-control" aria-describedby="basic-addon1">
				</td>
			</tr>
			<tr>
				<th width="10%">启用日期</th>
				<td width="40%">
					<input id="enableDate" type="text" class="form-control" aria-describedby="basic-addon1">
				</td>
			</tr>
			<tr>
				<th width="10%">机构职责描述</th>
				<td colspan="3">
					<textarea id="orgDuty" class="form-control" style="width:91.5%;height:70px;"></textarea>
				</td>
			</tr>
			<tr>
				<td colspan="4" style="text-align: center;">
					<a id="save" class="a-btn-submit" onclick="addOrgInfo()" >提 &nbsp;&nbsp;交</a>
					<a id="reset" class="a-btn-cancel" onclick="resetOrgInfo()" style="margin-left: 50px;">重 &nbsp;&nbsp;置</a>
				</td>
			</tr>
		</table>
		
	</div>
	<div class="tab-main">
		<table class="table-form" style="width: 100%;">
		 	<tr style="display: none;">
				<td width="40%">
					<input id="u_id" type="text" class="form-control inline"  aria-describedby="basic-addon1">
				</td>
			</tr>
			<tr>
				<th width="10%">机构编号</td>
				<td width="40%">
					<input id="u_orgCode" type="text" class="form-control inline"  aria-describedby="basic-addon1">
				</td>
				<th width="10%"><font color="red">*</font>机构名称</th>
				<td width="40%">
					<input id="u_orgName" type="text" class="form-control"  aria-describedby="basic-addon1">
				</td>
			</tr>
			<tr>
				<th width="10%"><font color="red">*</font>机构简称</th>
				<td width="40%">
					<input id="u_orgShortName" type="text" class="form-control"  aria-describedby="basic-addon1">
				</td>
				<th width="10%">归属机构名称</th>
				<td width="40%">
					<input id="u_parentOrgName" type="text" class="form-control" aria-describedby="basic-addon1">
				</td>
			</tr>
			<tr>
				<th width="10%">启用日期</th>
				<td width="40%">
					<input id="u_enableDate" type="text" class="form-control" aria-describedby="basic-addon1">
				</td>
			</tr>
			<tr>
				<th width="10%">机构职责描述</th>
				<td colspan="3">
					<textarea id="u_orgDuty" class="form-control" style="width:91.5%;height:70px;"></textarea>
				</td>
			</tr>
			<tr >
				<td colspan="4" style="text-align: center;">
					<a id="update" class="a-btn-submit" onclick="updateOrgInfo()">提 &nbsp;&nbsp;交</a>
				</td>
			</tr>
		</table>
	</div>
	<div class="tab-main" >	
		<div>
			<div>
				<font color="blue" style="font-size: 22px;color: #03A9F4">| </font>
				<font style="font-size: 16px;color: 444444">授权列表</font></th>
			</div>
			<div style="margin-top: 20px">
				<a class="a-btn-blue " onclick="confirmTempUsers()"><span class="glyphicon glyphicon-plus font-size-12px"></span>&nbsp;添&nbsp;&nbsp;加</a>	
				<a class="a-btn-red" onclick="confirmTempUsers()"><span class="glyphicon glyphicon-remove font-size-12px"></span>&nbsp;删&nbsp;&nbsp;除</a>	
			</div>			
		</div>
		<div style="margin-top: 20px">
			<table id="userManagerTempTable" class="table table-striped table-hover">
			  <thead>
			    <tr>
			      <th field="account">管理员账号</th>
			      <th field="name">管理员名称</th>
			      <th field="type">类别</th>
			      <th field="status">状态</th>
			    </tr>
			  </thead>
			  <tbody>
			  </tbody>
			</table>
			<div style="margin-bottom: 10px;">
			  <ul id="userManagerTempTable_page" class="pagination">
			  	<!-- 
			    <li><a>上一页</a></li>
			    <li class="active"><a>1</a></li>
			    <li><a>2</a></li>
			    <li><a>3</a></li>
			    <li><a>4</a></li>
			    <li><a>5</a></li>
			    <li><a>6</a></li>
			    <li><a>7</a></li>
			    <li><a>8</a></li>
			    <li><a>9</a></li>
			    <li><a>10</a></li>
			    <li><a>下一页</a></li>-->
			  </ul>
			   
			  <div class="pull-right" style="margin-top: 25px;padding-right: px">
			  	<!-- 显示分页参数  -->
			  </div>
			 </div>
		</div>
	</div>
</body>
<script type="text/javascript">

$(document).ready(function() {
	$(".tab").eq(0).click();
	
	$("#terminalTable").initGrid(15);
    query();
    generOrgTree();
});

var parentId;
var zTreeObj;
var level;
//zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var setting = {
	callback:{
		onClick:zTreeOnClick
	},
	view:{
		showLine:false
	}
};




function zTreeOnClick(event, treeId, treeNode){
	parentId=treeNode.id;
	level = treeNode.level;
	query();
}

//生成组织机构树
function generOrgTree(){ 
	$.ajax({
        type: "post",
        dataType: "json",
        url: '${ctx}/org/getOrgTree',
        success: function (data) {
        	zTreeObj = $.fn.zTree.init($("#orgTree"), setting, data);
        	
        	//var rootNode = zTreeObj.getNodeByTId("1");
        	//zTreeObj.selectNode(rootNode,true);		
        },
		error:function(){
		}
    });
	
}

function query(){
	var pageSize = parseInt(window["orgManagerTable_page"].pageSize);
	var pageNum = parseInt(window["orgManagerTable_page"].pageNum);
	
	$.ajax({
        type: "post",
        dataType: "json",
        url: '${ctx}/org/getOrgListWithParent',
        data:{
        	pageNum:pageNum,
        	pageSize:pageSize,
        	parentId:parentId,
        	parentOrgCode:$("#searchParentOrgCode").val(),
        	parentOrgName:$("#searchParentOrgName").val(),
        	orgShortName:$("#searchOrgShortName").val(),
        	searchField:$("#searchFieldOrg").val()
        },
        success: function (data) {
        	$("#orgManagerTable").loadGrid(data);
        },
		error:function(){
		}
    });
}


/**
 * 授权
 */
function queryUserAuthList(orgId){
	$.ajax({
        type: "post",
        dataType: "json",
        url: '${ctx}/sysUser/getUserAuthList',
        data:{
        	orgId:orgId
        },
        success: function (data) {
        	$("#orgManagerTable").loadGrid(data);
        },
		error:function(){
		}
    });
}

//添加
$("#btnAdd").on("click",function(){
	//parentId IsLastTree;
	if(parentId==null){
		$.MsgBox.Alert("提示","请选择上级部门");
	}else{
		level = level+1;
		$("#level").val(level);
		$(".tab").eq(1).click();
	}
});


function addOrgInfo(){
	
	var orgName = document.getElementById("orgName");
	var orgShortName = document.getElementById("orgShortName"); 
	
	if(orgName.value == null||orgName.value==""){
		$.MsgBox.Alert("提示","机构名称不能为空!");
		$("#orgName").focus();
	}else if(orgShortName.value == null||orgShortName.value==""){	
		$.MsgBox.Alert("提示","机构简称不能为空!");
		$("#orgShortName").focus();
	}else{
		$.MsgBox.Confirm("提示","确定要添加新增部门？",function(){
			var icon = null;
			level = $("#level").val();
			if(level=="1"){
				icon = "icons/ico_department.png";
			}else if(level=="2"){
				icon = "icons/ico_organization1.png";
			}else {
				icon = "icons/ico_people.png";
			}
			$.ajax({
		        type: "post",
		        dataType: "json",
		        url: '${ctx}/org/addOrg',
		        data:{
		        	orgCode:$("#orgCode").val(),
		        	orgName:$("#orgName").val(),
		        	orgShortName:$("#orgShortName").val(),
		        	orgDuty:$("#orgDuty").val(),
		        	enableDate:$("#enableDate").val(),
		        	parentId:parentId,
		        	level:$("#level").val(),
		        	icon:icon
		        },
		        success: function (data) {
		        	if(data.status=="1"){
			        	$.MsgBox.Alert("提示",data.msg);
			        	$(".tab-main tr td input").val("");
			        	generOrgTree();
			        	query();
			        	$(".tab").eq(0).click();
		        	}else{
		        		$.MsgBox.Alert("提示",data.msg);
		        	}
		        	
		        },
				error:function(){
					$.MsgBox.Alert("提示",data.msg);
				}
		    });
		});
	}
}

//新增用户信息重置
function resetUserInfo(){
	$.MsgBox.Confirm("提示","确定重置信息?",function(){
		$(".tab-main tr td input").val("");
		$("#account").focus();
	});
}

/**
 * 点击修改按钮跳转事件
 * 修改信息提交
 */

$("#btnEdit").on("click",function(){
	var result = $("#orgManagerTable").getSelections();
	if(parseInt(result.length)==0){
		$.MsgBox.Alert("提示","请选择一条记录!");
	}else if(parseInt(result.length)==1){
		$(".tab").eq(2).click();
		$("#u_id").val(result[0].id),
    	$("#u_orgCode").val(result[0].orgCode),
    	$("#u_orgName").val(result[0].orgName),
    	$("#u_orgShortName").val(result[0].orgShortName),
    	$("#u_parentOrgName").val(result[0].parentOrgName),
    	$("#u_enableDate").val(result[0].enableDate),
    	$("#u_orgDuty").val(result[0].orgDuty)		
	}else{
		$.MsgBox.Alert("提示","只能选择一条记录!");
	}
});

 function updateOrgInfo(){
	 	var orgName = document.getElementById("u_orgName");
		var orgShortName = document.getElementById("u_orgShortName"); 
		
		if(orgName.value == null||orgName.value==""){
			$.MsgBox.Alert("提示","机构名称不能为空!");
			$("#orgName").focus();
		}else if(orgShortName.value == null||orgShortName.value==""){	
			$.MsgBox.Alert("提示","机构简称不能为空!");
			$("#orgShortName").focus();
		}else{	
			$.MsgBox.Confirm("提示","确定要修改用户信息？",function(){
				$.ajax({
			        type: "post",
			        dataType: "json",
			        url: '${ctx}/org/updateOrg',
			        data:{
			        	id:$("#u_id").val(),
			        	orgCode:$("#u_orgCode").val(),
			        	orgName:$("#u_orgName").val(),
			        	orgShortName:$("#u_orgShortName").val(),
			        	parentOrgName:$("#u_parentOrgName").val(),
			        	enableDate:$("#u_enableDate").val(),
			        	orgDuty:$("#u_orgDuty").val()
			        },
			        success: function (data) {
			        	if(data.status=="1"){
			        		$.MsgBox.Alert("提示",data.msg); 
				        	query();
				        	$(".tab-main tr td input").val("");
				        	$(".tab").eq(0).click();
			        	}else{
			        		$.MsgBox.Alert("提示",data.msg);
			        		document.getElementById("account").focus();
			        	}
			        },
					error:function(){
						$.MsgBox.Alert("提示",data.msg);
					}
			    });
			});
		}
}

/**
 * 删除数据
 */
 $("#btnDel").on("click",function(){
 	var result = $("#orgManagerTable").getSelections();
 	var orgIds = [];
 	for(var i=0; i<result.length; i++){
 		orgIds.push(result[i].id);
 	}
 	if(parseInt(result.length)==0){
 		$.MsgBox.Alert("提示","请选择一条记录!"); 
 	}else{
 		$.MsgBox.Confirm("提示","确定删除？",function(){
 			$.ajax({
 	 	        type: "post",
 	 	        dataType: "json",
 	 	        url: '${ctx}/org/deleteOrgs',
 	 	        data:{
 	 	        	"orgIds":orgIds.join(",")
 	 	        },
 	 	        success: function (data) {
 	 	        	if(data.status=='1'){
 	 	        		$.MsgBox.Alert("提示",data.msg); 
 	 	        		generOrgTree();
 	 	        		query();
 	 	        	}else{
 	 	        		$.MsgBox.Alert("提示",data.msg);
 	 	        	}
 	 	        },
 	 			error:function(){
 	 				$.MsgBox.Alert("提示",data.msg);
 	 			}
 	 	    });
 		});
 	}
 });
 
 
 $("#advancedSearchbtnOrg").on("click",function(){
		 if($("#advancedSearchOrg").css('display')=="block"){
		    $("#advancedSearchOrgText").html('高级搜索');
			$("#advancedSearchOrgSpan").removeClass('glyphicon glyphicon-chevron-up');
			$("#advancedSearchOrgSpan").addClass('glyphicon glyphicon-chevron-down');
			$("#advancedSearchOrg").css("display","none");
			$("#searchParentOrgCode").val('');
        	$("#searchParentOrgName").val('');
        	$("#searchOrgShortName").val('')
		 }else{
		    $("#advancedSearchOrgText").html('收起条件');
			$("#advancedSearchOrgSpan").removeClass('glyphicon glyphicon-chevron-down');
			$("#advancedSearchOrgSpan").addClass('glyphicon glyphicon-chevron-up');
			$("#advancedSearchOrg").css("display","block");
		 }
	});
 
 $("#btnSearchOrg").click(function(){
	query();
});

</script>
</html>